<?php /* ADMIN $Id: viewuser.php,v 1.39.4.5 2006/07/27 19:16:07 nybod Exp $ */
GLOBAL $addPwT,$company_id, $dept_ids, $department, $min_view, $m, $a;
$user_id = isset( $_GET['user_id'] ) ? $_GET['user_id'] : 0;

if ($user_id != $AppUI->user_id
&& ( ! $perms->checkModuleItem('admin', 'view', $user_id)
|| ! $perms->checkModuleItem('users', 'view', $user_id) ) ) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
$AppUI->savePlace();

if (isset( $_POST['show_form'] )) {
	$add_pwt = dPgetParam($_POST, 'add_pwt', 0 );
	$AppUI->setState( 'addProjWithTasks', $add_pwt);
} else {
	$AppUI->setState( 'addProjWithTasks', false);
}

$addPwT = $AppUI->getState( 'addProjWithTasks' ) ? $AppUI->getState( 'addProjWithTasks' ) : 0;

$company_id = $AppUI->getState( 'UsrProjIdxCompany' ) !== NULL ? $AppUI->getState( 'UsrProjIdxCompany' ) : $AppUI->user_company;

$company_prefix = 'company_';

if (isset( $_POST['department'] )) {
	$AppUI->setState( 'UsrProjIdxDepartment', $_POST['department'] );

	//if department is set, ignore the company_id field
	unset($company_id);
}
$department = $AppUI->getState( 'UsrProjIdxDepartment' ) !== NULL ? $AppUI->getState( 'UsrProjIdxDepartment' ) : $company_prefix.$AppUI->user_company;

//if $department contains the $company_prefix string that it's requesting a company and not a department.  So, clear the
// $department variable, and populate the $company_id variable.
if(!(strpos($department, $company_prefix)===false)){
	$company_id = substr($department,strlen($company_prefix));
	$AppUI->setState( 'UsrProjIdxCompany', $company_id );
	unset($department);
}

if (isset( $_GET['tab'] )) {
	$AppUI->setState( 'UserVwTab', $_GET['tab'] );
}
$tab = $AppUI->getState( 'UserVwTab' ) !== NULL ? $AppUI->getState( 'UserVwTab' ) : 0;

// pull data
$q  = new DBQuery;
$q->addTable('users', 'u');
$q->addQuery('u.*');
$q->addQuery('con.*, company_id, company_name, dept_name, dept_id');
$q->addJoin('contacts', 'con', 'user_contact = contact_id');
$q->addJoin('companies', 'com', 'contact_company = company_id');
$q->addJoin('departments', 'dep', 'dept_id = contact_department');
$q->addWhere('u.user_id = '.$user_id);
$sql = $q->prepare();
$q->clear();

if (!db_loadHash( $sql, $user )) {
	$titleBlock = new CTitleBlock( 'Invalid User ID' );
	$titleBlock->addButton('users list', '?m=admin');
	$titleBlock->show();
} else {
	$titleBlock = new CTitleBlock( 'View User' );
	if ($canRead) {
		$titleBlock->addButton( 'users list', 'index.php?m=admin' );
	}
	if (1 == $AppUI->user_id) {
		$titleBlock->addButton($AppUI->_('add user'), 'index.php?m=admin&a=addedituser');
	}
	$titleBlock->show();
?>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
	<td class="tabon">
		<img src="images/kuser.png" class="ico">
		<?=$user['contact_first_name'].' '.$user['contact_last_name']?>
	</td>
	<td width="100%">&nbsp;</td>
</tr>
</table>
<?
$detail_class = 'infopanel';
if ($canEdit || $user_id == $AppUI->user_id) {
	$detail_class = 'subinfopanel';
?>
<script>
function doChangePassword() {
	var f = document.frmEdit;
	var msg = '';

	if (f.new_pwd1.value.length < <?=dPgetConfig('password_min_len')?>) {
		msg += "<?=$AppUI->_('chgpwValidNew', UI_OUTPUT_JS)?>" + <?=dPgetConfig('password_min_len')?>;
		f.new_pwd1.focus();
	}
	if (f.new_pwd1.value != f.new_pwd2.value) {
		msg += "\n<?=$AppUI->_('chgpwNoMatch', UI_OUTPUT_JS)?>";
		f.new_pwd2.focus();
	}
	if (msg.length < 1) {
		f.submit();
	} else {
		alert(msg);
	}
}

function ChangePassword() {
	this.show = function() {
		ajax.send('index.php?m=public&a=chpwd&suppressHeaders=1&user_id=<?=$user['user_id']?>', '',
			function(result) {
				div = document.createElement('div');
				document.body.appendChild(div);
				div.style.visibility = 'hidden';
				div.style.padding = '0px';
				div.className = 'popup';
				div.innerHTML = result;
				div.id = 'idChangePassword';

				Dialog(div.id).show();
			}
		);
	}

	return this;
}
</script>
<table border="0" cellpadding="4" cellspacing="0" width="100%" class="infopanel">
<tr>
	<td>
	<?
		showButton('edit information', '?m=admin&a=addedituser&user_id='.$user_id);
		echo '&nbsp;';
		showButton('edit preferences', '?m=system&a=addeditpref&user_id='.$user_id);
		echo '&nbsp;';
		showButton($AppUI->_('change password'), 'js:ChangePassword().show()');
	?>
	</td>
</tr>
</table>
<? } ?>
<table border="0" cellpadding="4" cellspacing="0" width="100%" class="<?=$detail_class?>">
<tr style="background-color: white">
	<td width="50%">
		<table cellspacing="1" cellpadding="2" border="0" width="100%">
		<tr>
			<td align="right"><?=$AppUI->_('Login')?>:</td>
			<td width="100%"><?=$user['user_username']?></td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Type')?>:</td>
			<td width="100%"><?=$AppUI->_($utypes[$user['user_type']])?></td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Company')?>:</td>
			<td width="100%">
				<a href="?m=companies&a=view&company_id=<?=@$user['contact_company']?>"><?=@$user['company_name']?></a>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Department')?>:</td>
			<td width="100%">
				<a href="?m=departments&a=view&dept_id=<?=@$user['contact_department']?>"><?=$user['dept_name']?></a>
			</td>
		</tr>
		<tr>
			<td align="right" valign="top"><?=$AppUI->_('Phone')?>:</td>
			<td width="100%">
				<img src="images/phone.gif" title="Mobile phone">&nbsp;<?=@$user['contact_mobile']?><br/>
				<img src="images/workphone.gif" title="Work phone">&nbsp;<?=@$user['contact_phone']?><br/>
				<img src="images/homephone.gif" title="Home phone">&nbsp;<?=@$user['contact_phone2']?>&nbsp;
			</td>
		</tr>
		<tr valign=top>
			<td align="right"><?=$AppUI->_('Address')?>:</td>
			<td width="100%"><?php
				echo @$user['contact_address1']
					.( ($user['contact_address2']) ? '<br />'.$user['contact_address2'] : '' )
					.'<br />'.$user['contact_city']
					.'&nbsp;&nbsp;'.$user['contact_state']
					.'&nbsp;&nbsp;'.$user['contact_zip']
					.'<br />'.$user['contact_country']
					;
			?></td>
		</tr>
		</table>

	</td>
	<td width="50%" valign="top">
		<table width="100%">
		<tr>
			<td align="right"><?=$AppUI->_('Birthday')?>:</td>
			<td width="100%"><?=@$user['contact_birthday'] != '0000-00-00' ? @$user['contact_birthday'] : '&nbsp;'?></td>
		</tr>
		<tr>
			<td align="right">ICQ#:</td>
			<td width="100%"><?=@$user['contact_icq']?></td>
		</tr>
		<tr>
			<td align="right">AOL:</td>
			<td width="100%"><a href="aim:<?=@$user['contact_aol']?>"><?=@$user['contact_aol']?></a></td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Email')?>:</td>
			<td width="100%"><?='<a href="mailto:'.@$user['contact_email'].'">'.@$user['contact_email'].'</a>'?></td>
		</tr>
		<tr>
			<td colspan="2"><?=$AppUI->_('Signature')?>:</td>
		</tr>
		<tr>
			<td width="100%" colspan="2">
				<?=str_replace( chr(10), '<br/>', $user['user_signature'])?>&nbsp;
			</td>
		</tr>
		</table>
	</td>
</tr>
</table>

<?php
	// tabbed information boxes
	$min_view = true;
	$tabBox = new CTabBox( "?m=admin&a=viewuser&user_id=$user_id", $tab );
	$tabBox->loadExtras('admin', 'viewuser');
	//$tabBox->add( 'vw_usr_log', 'User Log');
	$tabBox->add( 'vw_usr_perms', 'Permissions' );
	$tabBox->add( 'vw_usr_roles', 'Roles' );
	$tabBox->show();
}
?>
